﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xboxssbeatemup
{
    class TriggerableBehavior : Behavior
    {
        public PlayerIndex player = PlayerIndex.One;
        public enum ControllerType
        {
            Gamepad,
            Keyboard
        }
        public ControllerType controller = ControllerType.Gamepad;
        public Buttons gamepadTrigger = Buttons.X;
        public Keys keyboardTrigger = Keys.Space;

        GamePadState gamepadState;
        GamePadState previousGamepadState;
#if !XBOX
        KeyboardState keyboardState;
        KeyboardState previousKeyboardState;
#endif

        public TriggerableBehavior(Game game, GameObject t) : base(game,t)
        {
            target = t;
        }

        public TriggerableBehavior(Game game, GameObject t, params Object[] objects)
            : base(game, t)
        {
            foreach (Object o in objects)
            {
                if (o is PlayerIndex)
                {
                    player = (PlayerIndex)o;
                }
                if (o is Buttons)
                {
                    controller = ControllerType.Gamepad;
                    gamepadTrigger = (Buttons)o;
                }
                if (o is Keys)
                {
                    controller = ControllerType.Keyboard;
                    keyboardTrigger = (Keys)o;
                }
            }
        }

        public override void Initialize()
        {
            gamepadState = GamePad.GetState(player);
            previousGamepadState = GamePad.GetState(player);
#if !XBOX
            keyboardState = Keyboard.GetState();
            previousKeyboardState = Keyboard.GetState();
#endif
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            gamepadState = GamePad.GetState(player);
#if !XBOX
            keyboardState = Keyboard.GetState();
#endif

            switch (controller)
            {
                case ControllerType.Gamepad:
                    if (gamepadState.IsButtonDown(gamepadTrigger) && previousGamepadState.IsButtonUp(gamepadTrigger))
                    {
                        Fire();
                    }
                    break;
                case ControllerType.Keyboard:
#if !XBOX
                    if (keyboardState.IsKeyDown(keyboardTrigger) && previousKeyboardState.IsKeyUp(keyboardTrigger))
                    {
                        Fire();
                    }
#endif
                    break;
            }

            previousGamepadState = gamepadState;
#if !XBOX
            previousKeyboardState = keyboardState;
#endif

            base.Update(gameTime);
        }

        public virtual void Fire()
        {

        }
    }
}
